<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="报修编号" prop="repairNum">
        <el-input
          v-model="queryParams.repairNum"
          placeholder="请输入报修编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报修状态" prop="repairState">
        <el-input
          v-model="queryParams.repairState"
          placeholder="请输入报修状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="派单时间" prop="assignmentTime">
        <el-date-picker clearable
          v-model="queryParams.assignmentTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择派单时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="接单时间" prop="receivingOrdersTime">
        <el-date-picker clearable
          v-model="queryParams.receivingOrdersTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择接单时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="处理完成时间" prop="completeTime">
        <el-date-picker clearable
          v-model="queryParams.completeTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择处理完成时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="取消时间" prop="cancelTime">
        <el-date-picker clearable
          v-model="queryParams.cancelTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择取消时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="期待上门时间" prop="doorTime">
        <el-date-picker clearable
          v-model="queryParams.doorTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择期待上门时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="分派人id" prop="assignmentId">
        <el-input
          v-model="queryParams.assignmentId"
          placeholder="请输入分派人id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理人id" prop="completeId">
        <el-input
          v-model="queryParams.completeId"
          placeholder="请输入处理人id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理人电话" prop="completePhone">
        <el-input
          v-model="queryParams.completePhone"
          placeholder="请输入处理人电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理人姓名" prop="completeName">
        <el-input
          v-model="queryParams.completeName"
          placeholder="请输入处理人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入创建人id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="小区ID" prop="communityId">
        <el-input
          v-model="queryParams.communityId"
          placeholder="请输入小区ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="详细地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入详细地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:repair:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:repair:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:repair:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:repair:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="repairList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="报修ID" align="center" prop="repairId" />
      <el-table-column label="报修编号" align="center" prop="repairNum" />
      <el-table-column label="报修状态" align="center" prop="repairState" />
      <el-table-column label="派单时间" align="center" prop="assignmentTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.assignmentTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="接单时间" align="center" prop="receivingOrdersTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.receivingOrdersTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理完成时间" align="center" prop="completeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.completeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="取消时间" align="center" prop="cancelTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.cancelTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="期待上门时间" align="center" prop="doorTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.doorTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分派人id" align="center" prop="assignmentId" />
      <el-table-column label="处理人id" align="center" prop="completeId" />
      <el-table-column label="处理人电话" align="center" prop="completePhone" />
      <el-table-column label="处理人姓名" align="center" prop="completeName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建人id" align="center" prop="userId" />
      <el-table-column label="报修内容" align="center" prop="repairContent" />
      <el-table-column label="小区" align="center" prop="community.communityName" />
      <el-table-column label="详细地址" align="center" prop="address" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:repair:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:repair:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-success"
            @click="handleComplete(scope.row)"
            v-hasPermi="['system:repair:complete']"
          >完成</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-close"
            @click="handleReject(scope.row)"
            v-hasPermi="['system:repair:reject']"
          >驳回</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改报修信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="报修编号" prop="repairNum">
          <el-input v-model="form.repairNum" placeholder="请输入报修编号" />
        </el-form-item>
        <el-form-item label="报修状态" prop="repairState">
          <el-input v-model="form.repairState" placeholder="请输入报修状态" :readonly="!isEdit" />
        </el-form-item>
        <el-form-item label="派单时间" prop="assignmentTime">
          <el-date-picker clearable
            v-model="form.assignmentTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择派单时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="接单时间" prop="receivingOrdersTime">
          <el-date-picker clearable
            v-model="form.receivingOrdersTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择接单时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="处理完成时间" prop="completeTime">
          <el-date-picker clearable
            v-model="form.completeTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择处理完成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="取消时间" prop="cancelTime">
          <el-date-picker clearable
            v-model="form.cancelTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择取消时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="期待上门时间" prop="doorTime">
          <el-date-picker clearable
            v-model="form.doorTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择期待上门时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="分派人id" prop="assignmentId">
          <el-input v-model="form.assignmentId" placeholder="请输入分派人id" />
        </el-form-item>
        <el-form-item label="处理人id" prop="completeId">
          <el-input v-model="form.completeId" placeholder="请输入处理人id" />
        </el-form-item>
        <el-form-item label="处理人电话" prop="completePhone">
          <el-input v-model="form.completePhone" placeholder="请输入处理人电话" />
        </el-form-item>
        <el-form-item label="处理人姓名" prop="completeName">
          <el-input v-model="form.completeName" placeholder="请输入处理人姓名" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="创建人id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入创建人id" />
        </el-form-item>
        <el-form-item label="删除状态0默认1删除" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除状态0默认1删除" />
        </el-form-item>
        <el-form-item label="报修内容">
          <editor v-model="form.repairContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="小区ID" prop="communityId">
          <el-input v-model="form.communityId" placeholder="请输入小区ID" />
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRepair,
  getRepair,
  delRepair,
  addRepair,
  updateRepair,
  reviewRepair,
  completeRepair,
  rejectRepair
} from "@/api/zy/repair";

export default {
  name: "Repair",
  data() {
    return {
      // 遮罩层
      loading: true,
      repairState: "审核中",
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报修信息表格数据
      repairList: [
        {
          community:{}
        }
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        repairNum: null,
        repairState: null,
        assignmentTime: null,
        receivingOrdersTime: null,
        completeTime: null,
        cancelTime: null,
        doorTime: null,
        assignmentId: null,
        completeId: null,
        completePhone: null,
        completeName: null,
        userId: null,
        repairContent: null,
        communityId: null,
        address: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        repairNum: [
          { required: true, message: "报修编号不能为空", trigger: "blur" }
        ],
        repairState: [
          { required: true, message: "报修状态不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询报修信息列表 */
    getList() {
      this.loading = true;
      listRepair(this.queryParams).then(response => {
        this.repairList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        repairId: null,
        repairNum: null,
        repairState: "审核中",
        assignmentTime: null,
        receivingOrdersTime: null,
        completeTime: null,
        cancelTime: null,
        doorTime: null,
        assignmentId: null,
        completeId: null,
        completePhone: null,
        completeName: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        userId: null,
        delFlag: null,
        repairContent: null,
        communityId: null,
        address: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.repairId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加报修信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.isEdit = true; // 设置为修改状态
      const repairId = row.repairId || this.ids;
      getRepair(repairId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改报修信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.repairId != null) {
            updateRepair(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRepair(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const repairIds = row.repairId || this.ids;
      this.$modal.confirm('是否确认删除报修信息编号为"' + repairIds + '"的数据项？').then(function() {
        return delRepair(repairIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/repair/export', {
        ...this.queryParams
      }, `repair_${new Date().getTime()}.xlsx`)
    },

    handleComplete(row) {
      this.$confirm('是否确认完成报修编号为"' + row.repairNum + '"的报修信息?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return completeRepair(row.repairId, "已完成");
      }).then(() => {
        this.getList();
        this.$message.success("报修处理成功");
      }).catch(() => {});
    },

    /** 驳回报修 */
    handleReject(row) {
      this.$prompt('请输入驳回原因', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputType: 'textarea',
        inputPlaceholder: '请输入驳回原因'
      }).then(({ value }) => {
        return rejectRepair(row.repairId, value);
      }).then(() => {
        this.getList();
        this.$message.success("报修驳回成功");
      }).catch(() => {});
    }
  }
};
</script>
